﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Base.cshtml";
}
@section head{
    <script src="/content/scripts/modernizr-2.6.2.min.js" type="text/javascript"></script>
}
<table border="0" width="100%" style="margin-top:5px">
    <tr>
        <td>
            <span style="font-size: 14px">关键字:</span>
            <input id="txtKey" name="txtKey" type="text" class="input-medium search-query" placeholder="流程名称"
                style="width: 250px; background-color: #fff;" />
            <button class="btn btn-primary btn-small" id="btnSearch">
                查询
            </button>
        </td>
        <td align="right">
            <button id="btnAdd" class="btn btn-primary btn-small" style="margin-right: 20px">
                新增流程
            </button>
        </td>
    </tr>
</table>
<table id="processdefgrid" class="mmg">
    <tr>
        <th rowspan="" colspan="">
        </th>
    </tr>
</table>
<div id="pg" style="text-align: right;">
</div>
<div id="processDef" class="modal fade" data-backdrop="static" tabindex="-1" style="display: none;
    width: 400px">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
            ×</button>
        <h3 class="modal-title" style="font-size: 17px; font-weight: bold" id="modaltitle">
            新增流程</h3>
    </div>
    @*<div class="modal-body" style="z-index:9999">*@
    <table border="0" cellpadding="0" cellspacing="0" style="margin: 10px">
        <tr>
            <td valign="middle" width="100px" align="right">
                流程名称:
            </td>
            <td>
                <input type="text" id="txtProcessDefName" placeholder="流程名称" class="col-xs-10 col-sm-5" /><span
                    style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                流程类型:
            </td>
            <td>
                <select id="selType" name="selType" class="selectpicker" data-style="btn-info">
                    <option value="1">项目流程</option>
                    <option value="2">其他流程</option>
                </select>
                <span style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                &nbsp;
            </td>
            <td>
                <span id="lblmsg" style="color: Red">&nbsp;</span>
                <input type="hidden" name="processdefID" id="processdefID" value="0" />
            </td>
        </tr>
    </table>
    @*</div>*@
    <div class="modal-footer">
        <button id="btnSave" type="button" class="btn btn-primary">
            保存</button>
        <button type="button" data-dismiss="modal" class="btn btn-default">
            取消</button>
    </div>
</div>
@section tail{
    <script src="/content/plugins/mmgrid/mmGrid.js" type="text/javascript"></script>
    <script src="/content/plugins/mmgrid/mmPaginator.js" type="text/javascript"></script>
    <script type="text/javascript">
        var processDefDiv = $("#processDef");
        var cols = [
        { title: '名称', name: 'ProcessDefName', width: 300, align: 'center' },
        { title: '类型', name: 'ProcessType', width: 80, align: 'center',renderer: function (val, item, index) {
            if (val == 1) return '项目流程';
            return '其他流程';
            } },
        { title: '状态', name: 'Status', width: 80, align: 'center', renderer: function (val, item, index) {
            if (val == 0) return '禁用';
            return '正常';
            }
        },
        { title: '操作', name: 'ID', width: 400, align: 'center', lockWidth: true, lockDisplay: true, renderer: function (val, item, index) {
            var html = '<button  class="btn btn-info btn-mini" onclick="grid_processdef_edit(\'' + item["ID"] + '\',\'' + item["ProcessDefName"] + '\',\'' + item["ProcessType"] + '\')">编辑</button>';
            
            if (item["Status"] == 1) {
                html = html + ' <button  class="btn btn-info btn-mini" onclick="grid_processdef_changestate(\'' + item["ID"] + '\',0)">禁用</button>';
            }
            else {
                html = html + '<button  class="btn btn-info btn-mini" onclick="grid_processdef_changestate(\'' + item["ID"] + '\',1)">启用</button>';
            }
            html = html + ' <button  class="btn btn-danger btn-mini" onclick="grid_processdef_changestate(\'' + item["ID"] + '\',-1)">删除</button>';
            html = html + ' <button  class="btn btn-info btn-mini" onclick="openProcess(\'' + item["ID"] + '\',\'' + item["ProcessType"] + '\')">流程节点</button>';
            html = html + ' <button  class="btn btn-info btn-mini" onclick="openProcessStep(\'' + item["ID"] + '\',\'' + item["ProcessType"] + '\')">逻辑条件</button>';
            return html;
        }
        }
    ];
    var mmg;
    var seltype;
        $(function () {
            seltype=$('.selectpicker').selectpicker('show');
            mmg = $('#processdefgrid').mmGrid({
                indexCol: true,
                indexColWidth: 25,
                height: $("body", parent.document).height() - 220,
                cols: cols,
                url: '/Process/GetProcessDefListByPage',
                method: 'get',
                remoteSort: true,
                //items: items,
                //sortName: 'SECUCODE',
                //sortStatus: 'asc',
                multiSelect: false,
                checkCol: false,
                fullWidthRows: true,
                autoLoad: false,
                plugins: [
                        $('#pg').mmPaginator()
                    ]
            });
            mmg.load();

           

            $("#btnAdd").click(function () {
                resetform();
                // var userdiv = $("#processDef");
                var modaltitle = $("#modaltitle");
                modaltitle.text("新增流程");

                processDefDiv.modal();
            });
            $("#btnSave").click(function () {
                //save();
                //bootbox.alert("<h3>保存成功</h3>");
                $("#btnSave").text("保存中...");
                $("#btnSave").attr("disabled", true);
                save();

            });

            $("#btnSearch").click(function () {

                search();

            });
        });
        function search() {
            mmg.setParm({ key: $("#txtKey").val() });
            mmg.load({ page: 1 });
        }

        function save() {
            if ($('#txtProcessDefName').val() == "") {
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                $('#txtProcessDefName').focus();
                $("#lblmsg").html('流程名称不能为空');
                return;
            }
           
            $.ajax({
                url: "/Process/ProcessDefSave",
                data: "processDefName=" + $('#txtProcessDefName').val() + "&processDefType=" + seltype.val()
                    + "&defID=" + $("#processdefID").val(),
                type: "post",
                success: function (result) {
                    //写提交ajax操作
                    if (result == "1") {
                        processDefDiv.modal("hide");
                        bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class=' icon-ok-sign bigger-290'></i>&nbsp;&nbsp;流程保存成功</span>", '300px', function () {

                            search();
                        });
                        resetform();
                    }
                    else {
                        if (result == "-1") {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-warning-sign bigger-290'></i>&nbsp;&nbsp;流程名已存在</span>", '300px');

                        }
                        else {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-remove-sign bigger-290'></i>&nbsp;&nbsp;流程名已存在</span>", '300px');
                        }
                    }
                    $("#btnSave").text("保存");
                    $("#btnSave").attr("disabled", false);
                }
            });
            //processDefDiv.modal("hide");
        }

        function grid_processdef_edit(processdefID,processdefName, type) {
            //alert(processdefID + "HH" + processdefName + "MM" + type);
            setfrom(processdefID, processdefName, type);
            
            var modaltitle = $("#modaltitle");
            modaltitle.text("编辑流程");
            processDefDiv.modal();
        }

        function grid_processdef_changestate(processdefID, state) {

            var statestr = "禁用";
            if (state == 1) {
                statestr = "启用";
            }
            else if (state == -1) {
                statestr = "删除";
            }
            bootbox.confirm("<span style='font-size:14px;'><i style='color:green;' class='icon-question-sign bigger-290'></i> &nbsp;&nbsp;确定要" + statestr + "该流程吗？</span>", '300px', function (confirmed) {
                if (confirmed == true) {
                    $.ajax({
                        url: '/process/ProcessDefChangeStatus',
                        data: "processDefID=" + processdefID + "&status=" + state,
                        type: "post",
                        cache: false,
                        success: function (result) {

                            if (result == "1") {
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-ok-sign bigger-290'></i>&nbsp;&nbsp;" + statestr + "流程成功</span>", '300px');
                                mmg.load();
                            } else {
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-remove-sign bigger-290'></i>&nbsp;&nbsp;" + statestr + "流程失败</span>", '300px');

                            }
                        }
                    });
                }
            });

        }

        function openProcess(defID, type) {
            location.href = decodeURIComponent("/Process/ProcessProcList?defID=" + defID + "&processDefType=" + type);
        }
        function openProcessStep(defID, type) {
            location.href = decodeURIComponent("/Process/ProcessStepList?defID=" + defID + "&processDefType=" + type);
        }

        function resetform() {
            seltype.val("1");
            seltype.selectpicker('render');
            $("#txtProcessDefName").val(""); 
            $("#processdefID").val("0");
            $("#lblmsg").html("");
        }

        function setfrom(processdefID, processdefName, type) {
            seltype.val(type);
            seltype.selectpicker('render');
            
            $("#txtProcessDefName").val(processdefName);
            $("#processdefID").val(processdefID);
            $("#lblmsg").html("");
        }
    </script>
}
